/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thienloc.omap.controller.services;

import com.thienloc.omap.controller.BaseBean;
import com.thienloc.omap.entity.KetResult;
import com.thienloc.omap.entity.LotterySchedule;
import com.thienloc.omap.session.KetResultFacade;
import com.thienloc.omap.session.LotteryScheduleFacade;
import com.thienloc.omap.session.ServiceConfigFacade;
import com.thienloc.omap.util.JsfUtil;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

/**
 *
 * @author GiangLT
 */
@ManagedBean
@ViewScoped
public class KetCalculatorBean extends BaseBean implements Serializable {

    @EJB
    protected LotteryScheduleFacade lotteryScheduleFacade;
    @EJB
    private ServiceConfigFacade facade;
    @EJB
    private KetResultFacade ketResultFacade;
    private Integer doDaiCau;
    private Integer soNgayThongKe;
    private String maTinh;
    private Date startDate;
    private Date endDate;
    private final int SO_NGAY_AN = 1;
    private Map<String, String> provinceMap = new TreeMap<String, String>();
    private final Logger logger = LoggerFactory.getLogger(KetCalculatorBean.class);
    private List<KetResult> list = new ArrayList<KetResult>();

    @PostConstruct
    public void init() {
        try {
            Calendar calendar = Calendar.getInstance();
            startDate = calendar.getTime();
            onChangeStartDate();
        } catch (Exception ex) {
            logger.error("[{}]: ", getUsername(), ex);
            JsfUtil.addErrorMessage("Lỗi!", ex.getMessage());
        }
    }

    public void onChangeStartDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);

        calendar.add(Calendar.DAY_OF_MONTH, 1);
        endDate = calendar.getTime();// cho tuy chinh

        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);

        List<LotterySchedule> lotterySchedules = lotteryScheduleFacade.findByDayOfWeek(dayOfWeek);
        provinceMap = new TreeMap<String, String>();
        try {
            for (LotterySchedule lotterySchedule : lotterySchedules) {
                provinceMap.put(lotterySchedule.getProvinceId().getTenTinh() + " - " + lotterySchedule.getProvinceId().getMaTinh(), lotterySchedule.getProvinceId().getMaTinh());
            }
        } catch (Exception ex) {
            String error = "Lỗi! ";
            logger.error("[{}]: ", getUsername(), ex);
            JsfUtil.addErrorMessage(error, ex.getMessage());
        }
    }

    public void onChangeEndDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(endDate);

        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        List<LotterySchedule> lotterySchedules = lotteryScheduleFacade.findByDayOfWeek(dayOfWeek);
        provinceMap = new TreeMap<String, String>();
        try {
            for (LotterySchedule lotterySchedule : lotterySchedules) {
                provinceMap.put(lotterySchedule.getProvinceId().getTenTinh() + " - " + lotterySchedule.getProvinceId().getMaTinh(), lotterySchedule.getProvinceId().getMaTinh());
            }
        } catch (Exception ex) {
            String error = "Lỗi! ";
            logger.error("[{}]: ", getUsername(), ex);
            JsfUtil.addErrorMessage(error, ex.getMessage());
        }
    }

    public void search() {
        try {
            list = ketResultFacade.findByMaTinh(maTinh);
            Collections.sort(list, new Comparator<KetResult>() {

                @Override
                public int compare(KetResult o1, KetResult o2) {
                    if (Integer.valueOf(o1.getRating()) < Integer.valueOf(o2.getRating())) {
                        return 1;
                    }
                    if (Integer.valueOf(o1.getRating()) > Integer.valueOf(o2.getRating())) {
                        return -1;
                    }

                    if (Integer.valueOf(o1.getKetResultPK().getCauNumber()) > Integer.valueOf(o2.getKetResultPK().getCauNumber())) {
                        return 1;
                    }
                    if (Integer.valueOf(o1.getKetResultPK().getCauNumber()) < Integer.valueOf(o2.getKetResultPK().getCauNumber())) {
                        return -1;
                    }
                    return 0;
                }
            });
        } catch (Exception ex) {
            logger.error("[{}]: ", getUsername(), ex);
            JsfUtil.addErrorMessage("Lỗi!", ex.getMessage());
        }
    }

    public void calculate() {
        try {
            facade.tinhKet(maTinh, doDaiCau, SO_NGAY_AN, soNgayThongKe, startDate, endDate);
            JsfUtil.addSuccessMessage("Thành công", "Đã tính toán thành công");
            list = ketResultFacade.findByMaTinh(maTinh);
            Collections.sort(list, new Comparator<KetResult>() {

                @Override
                public int compare(KetResult o1, KetResult o2) {
                    if (Integer.valueOf(o1.getRating()) < Integer.valueOf(o2.getRating())) {
                        return 1;
                    }
                    if (Integer.valueOf(o1.getRating()) > Integer.valueOf(o2.getRating())) {
                        return -1;
                    }

                    if (Integer.valueOf(o1.getKetResultPK().getCauNumber()) > Integer.valueOf(o2.getKetResultPK().getCauNumber())) {
                        return 1;
                    }
                    if (Integer.valueOf(o1.getKetResultPK().getCauNumber()) < Integer.valueOf(o2.getKetResultPK().getCauNumber())) {
                        return -1;
                    }
                    return 0;
                }
            });
        } catch (Exception ex) {
            logger.error("[{}]: ", getUsername(), ex);
            JsfUtil.addErrorMessage("Lỗi!", ex.getMessage());
        }
    }

    public boolean rollbackacble() {
        Calendar current = Calendar.getInstance();
        int hour = current.get(Calendar.HOUR_OF_DAY);
        if (hour > 12 && hour < 20) {
            return false;
        } else {
            return true;
        }
    }

    public void rollback() {
        try {
            Calendar current = Calendar.getInstance();
            int hour = current.get(Calendar.HOUR_OF_DAY);
            int soNgay = 0;
            if (hour >= 0 && hour <= 12) {
                soNgay = 1;
            }
            facade.rollbackCau(soNgay);
            JsfUtil.addSuccessMessage("Thành công", "Đã rollback thành công");
            list = ketResultFacade.findByMaTinh(maTinh);
        } catch (Exception ex) {
            logger.error("[{}]: ", getUsername(), ex);
            JsfUtil.addErrorMessage("Lỗi!", ex.getMessage());
        }
    }

    public Integer getDoDaiCau() {
        return doDaiCau;
    }

    public void setDoDaiCau(Integer doDaiCau) {
        this.doDaiCau = doDaiCau;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public String getMaTinh() {
        return maTinh;
    }

    public void setMaTinh(String maTinh) {
        this.maTinh = maTinh;
    }

    public Map<String, String> getProvinceMap() {
        return provinceMap;
    }

    public void setProvinceMap(Map<String, String> provinceMap) {
        this.provinceMap = provinceMap;
    }

    public List<KetResult> getList() {
        return list;
    }

    public Integer getSoNgayThongKe() {
        return soNgayThongKe;
    }

    public void setSoNgayThongKe(Integer soNgayThongKe) {
        this.soNgayThongKe = soNgayThongKe;
    }
}
